<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Rechange extends Api
{
    /**
     * 创建充值订单
     *
     * @return \think\Response
     */
    public function create()
    {
        $res = is_buy($this->userId);
        if(!$res['status']) {
            return $res;
        }
        $rechange_model = model('Rechange');
        $rechange_mansong_model = model('RechangeMansong');
        $pay_amount = $this->request->post('pay_amount');
        //计算满减
        /*$mansong_condition = [
            ['status', '=', 1],
            ['man', 'elt', $pay_amount]
        ];
        $mansong = $rechange_mansong_model->where($mansong_condition)->select();
        $song = !empty($mansong)? $mansong->song : 0;*/
        $song = 0;
        if($pay_amount >= 10000){
            $song = 5000;
        }else if($pay_amount >= 5000){
            $song = 2000;
        }/*else if($pay_amount >= 3000){
            $song = 500;
        }*/else if($pay_amount >= 2000){
            $song = 0;
        }else if($pay_amount >= 1000){
            $song = 0;
        }
        //创建订单
        $insert_data = [
            'amount'        =>  $pay_amount + $song,
            'pay_amount'    =>  $pay_amount,
            'user_id'       => $this->userId,
            'status'        =>  0,
            'memo'          =>  $this->request->post('memo', '')
        ];
        if($rechange_model->save($insert_data)) {
            $this->result['msg'] = '订单创建成功';
            $this->result['data'] = $rechange_model->id;
            return $this->result;
        } else {
            $this->error['msg'] = '订单创建失败';
            return $this->error;
        }
    }
    /**
     * 获取充值列表
     * @return type
     */
    public function rechangeList()
    {
        $rechange_model = model('Rechange');
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', 20);
        $condition = [
            'status'    =>  $this->request->get('status', 1),
            'user_id'   =>  $this->userId
        ];
        $this->result['data'] = $rechange_model->where($condition)->limit(($page-1)*$size, $size)->order('id', 'desc')->select();
        return $this->result;
    }

    public function  getRechange(){
        $rechange_model = model('Rechange');
        $entity = $rechange_model->field('*')->where(['id' => $this->request->post('id', '')])->find();
        $this->result['msg'] = '获取成功成功';
        $this->result['data'] = $entity;
        return $this->result;
    }
}
